﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using XMK.Tadong.Model;
using XMK.Tadong.Biz;
using HU8.Web.FileExplorer.FileExplorer.WebServices;
using System.Configuration;
using HU8.Helpers.Files;
using HU8.Web.FileExplorer;
using System.Data;


namespace XMK.Tadong.Web.WebFileExplorer.DataSubmit
{
    public partial class DataSubmit : FileExplorerPage
    {
        #region 变量

        //获取文档存储路径
        string dataPath = string.Empty;


        #endregion 变量

        #region 事件处理程序

        //提交点击
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitData();
            }
        }

        //提交按钮点击事件
        protected void btnSunmit_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.Request.QueryString["File_ID"]))
            {
                TB_TEAM_FILEInfo model = new TB_TEAM_FILEInfo();
                TB_TEAM_FILEBiz bll = new TB_TEAM_FILEBiz();
                model.FILE_ID = Convert.ToInt32(this.Request.QueryString["File_ID"]);
                model.TEAM_ID = Convert.ToInt32(hidIDS.Value);
                model.FILE_NAME = txtWDMC.Text.Trim();
                string file_name = model.FILE_NAME.ToString();
                string FILE_EXT =file_name.Substring(file_name.IndexOf(".") + 1, file_name.Length);
                EXTBiz extbiz = new EXTBiz();
                ExtTypeinfo extmode = extbiz.getExtCodeByExtent(FILE_EXT);
                FILE_EXT = extmode.EXT_CODE;
                //if (FILE_EXT.Equals("doc") || FILE_EXT.Equals("docx") || FILE_EXT.Equals("DOC") || FILE_EXT.Equals("DOCX"))
                //{
                //    model.FILE_EXT = 2;

                //}
                //else if (FILE_EXT.Equals("xls") || FILE_EXT.Equals("xlsx") || FILE_EXT.Equals("XLS") || FILE_EXT.Equals("XLSX"))
                //{
                //    model.FILE_EXT = 3;

                //}
                //else if (FILE_EXT.Equals("pdf") || FILE_EXT.Equals("PDF"))
                //{
                //    model.FILE_EXT = 4;

                //}
                //else if (FILE_EXT.Equals("txt") || FILE_EXT.Equals("TXT"))
                //{
                //    model.FILE_EXT = 5;

                //}
                //else if (FILE_EXT.Equals("ppt") || FILE_EXT.Equals("pptx") || FILE_EXT.Equals("PPT") || FILE_EXT.Equals("PPTX"))
                //{
                //    model.FILE_EXT = 6;

                //}
                model.FILE_ABST = txtZYXX.Text.Trim();
                model.FILE_AUTHOR = txtZZ.Text.Trim();
                model.KEYW = txtGJC.Text.Trim();
                model.FILE_CRT_TIME = Convert.ToDateTime(Request.Form["txtBXRQ"].ToString());
                //model.FILE_VERSION = "";
                if (!txtMB.Text.Trim().Equals(string.Empty)) model.DEST_OBJ = txtMB.Text.Trim();
                if (!txtYWDX.Text.Trim().Equals(string.Empty)) model.BIZ_OBJ =txtYWDX.Text.Trim();
                model.SECRET_LVL ="";
                model.LANG_TYPE = 0;
                model.FILE_TYPE = "";
                model.SAVE_PATH = "Group/" + model.TEAM_ID + "/待审核";
                model.USER_ID = "1";
                model.STATUS = 0;
                model.CREATE_TIME = Convert.ToDateTime(System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                model.UPD_TIME = Convert.ToDateTime(System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
                if (bll.Update(model))
                {
                    lblError.Text = "提交成功";
                }
            }
            else
            {

                string teams = hidIDS.Value.Trim();
                teams = teams.TrimEnd(';');
                string[] team = teams.Split(';');
                if (!string.IsNullOrEmpty(team[0]))
                {
                    TB_TEAM_FILEInfo model = new TB_TEAM_FILEInfo();
                    TB_TEAM_FILEBiz bll = new TB_TEAM_FILEBiz();
                    model.TEAM_ID = Convert.ToInt32(team[0].ToString());
                    model.FILE_NAME = txtWDMC.Text.Trim();
                    string file_name = model.FILE_NAME.ToString();
                    string FILE_EXT =file_name.Substring(file_name.IndexOf(".") + 1);
                    EXTBiz extbiz = new EXTBiz();
                    ExtTypeinfo extmode = extbiz.getExtCodeByExtent(FILE_EXT);
                    FILE_EXT = extmode.EXT_CODE;
                    //if (FILE_EXT.Equals("doc") || FILE_EXT.Equals("docx") || FILE_EXT.Equals("DOC") || FILE_EXT.Equals("DOCX"))
                    //{
                    //    model.FILE_EXT = 2;

                    //}
                    //else if (FILE_EXT.Equals("xls") || FILE_EXT.Equals("xlsx") || FILE_EXT.Equals("XLS") || FILE_EXT.Equals("XLSX"))
                    //{
                    //    model.FILE_EXT = 3;

                    //}
                    //else if (FILE_EXT.Equals("pdf") || FILE_EXT.Equals("PDF"))
                    //{
                    //    model.FILE_EXT = 4;

                    //}
                    //else if (FILE_EXT.Equals("txt") || FILE_EXT.Equals("TXT"))
                    //{
                    //    model.FILE_EXT = 5;

                    //}
                    //else if (FILE_EXT.Equals("ppt") || FILE_EXT.Equals("pptx") || FILE_EXT.Equals("PPT") || FILE_EXT.Equals("PPTX"))
                    //{
                    //    model.FILE_EXT = 6;

                    //}
                    //else
                    //{
                    //    model.FILE_EXT = 1;
                    //}
                    model.FILE_ABST = txtZYXX.Text.Trim();
                    model.FILE_AUTHOR = txtZZ.Text.Trim();
                    model.KEYW = txtGJC.Text.Trim();
                    model.FILE_CRT_TIME = Convert.ToDateTime(Request.Form["txtBXRQ"].ToString());
                    //model.FILE_VERSION = "";
                    if (!txtMB.Text.Trim().Equals(string.Empty)) model.DEST_OBJ = txtMB.Text.Trim();
                    if (!txtYWDX.Text.Trim().Equals(string.Empty)) model.BIZ_OBJ = txtYWDX.Text.Trim();
                    model.SECRET_LVL = "";
                    model.LANG_TYPE = 0;
                    model.FILE_TYPE = "";
                    model.SAVE_PATH = "Group/" + model.TEAM_ID + "/待审核";
                    model.USER_ID = "1";
                    model.STATUS = 0;
                    model.CREATE_TIME = Convert.ToDateTime(System.DateTime.Now.ToString("yyyy-MM-dd"));
                    model.UPD_TIME = Convert.ToDateTime(System.DateTime.Now.ToString("yyyy-MM-dd"));


                    if (!bll.Add(model).Equals("0"))
                    {
                        //将文件复制到相应的课题组待审核的文件夹下
                        string newPath = ConfigurationManager.AppSettings["DiskPath"];
                        newPath += "Group/" + model.TEAM_ID + "/待审核";
                        string path = hidPath.Value;

                        string[] swfNames = path.Split('.');

                        string swfName = swfNames[0] + ".swf";
                        string pdffName = swfNames[0] + ".pdf";

                        FileHelper.Copy(path, newPath);
                        FileHelper.Copy(swfName, newPath);
                        FileHelper.Copy(pdffName, newPath);
                        lblError.Text = "提交成功";
                    }
                }
                else
                {

                    for (int i = 0; i < cbTeamList.Items.Count; i++)
                    {
                        if (cbTeamList.Items[i].Selected)
                        {

                            TB_TEAM_FILEInfo model = new TB_TEAM_FILEInfo();
                            TB_TEAM_FILEBiz bll = new TB_TEAM_FILEBiz();
                            model.TEAM_ID = Convert.ToInt32(cbTeamList.Items[i].Value);
                            model.FILE_NAME = txtWDMC.Text.Trim();
                            string file_name = model.FILE_NAME.ToString();
                            
                            string FILE_EXT =file_name.Substring(file_name.IndexOf(".") + 1);
                            EXTBiz extbiz = new EXTBiz();
                            ExtTypeinfo extmode = extbiz.getExtCodeByExtent(FILE_EXT);
                            FILE_EXT = extmode.EXT_CODE;
                            //if (FILE_EXT.Equals("doc") || FILE_EXT.Equals("docx") || FILE_EXT.Equals("DOC") || FILE_EXT.Equals("DOCX"))
                            //{
                            //    model.FILE_EXT = 2;

                            //}
                            //else if (FILE_EXT.Equals("xls") || FILE_EXT.Equals("xlsx") || FILE_EXT.Equals("XLS") || FILE_EXT.Equals("XLSX"))
                            //{
                            //    model.FILE_EXT = 3;

                            //}
                            //else if (FILE_EXT.Equals("pdf") || FILE_EXT.Equals("PDF"))
                            //{
                            //    model.FILE_EXT = 4;

                            //}
                            //else if (FILE_EXT.Equals("txt") || FILE_EXT.Equals("TXT"))
                            //{
                            //    model.FILE_EXT = 5;

                            //}
                            //else if (FILE_EXT.Equals("ppt") || FILE_EXT.Equals("pptx") || FILE_EXT.Equals("PPT") || FILE_EXT.Equals("PPTX"))
                            //{
                            //    model.FILE_EXT = 6;

                            //}
                            //else
                            //{
                            //    model.FILE_EXT = 1;
                            //}
                            model.FILE_ABST = txtZYXX.Text.Trim();
                            model.FILE_AUTHOR = txtZZ.Text.Trim();
                            model.KEYW = txtGJC.Text.Trim();
                            model.FILE_CRT_TIME = Convert.ToDateTime(Request.Form["txtBXRQ"].ToString());
                            //model.FILE_VERSION = "";
                            if (!txtMB.Text.Trim().Equals(string.Empty)) model.DEST_OBJ = txtMB.Text.Trim();
                            if (!txtYWDX.Text.Trim().Equals(string.Empty)) model.BIZ_OBJ = txtYWDX.Text.Trim();
                            model.SECRET_LVL = "";
                            model.LANG_TYPE = 0;
                            model.FILE_TYPE = "";
                            model.SAVE_PATH = "Group/" + model.TEAM_ID + "/待审核";
                            model.USER_ID = "1";
                            model.STATUS = 0;
                            model.CREATE_TIME = Convert.ToDateTime(System.DateTime.Now.ToString("yyyy-MM-dd"));
                            model.UPD_TIME = Convert.ToDateTime(System.DateTime.Now.ToString("yyyy-MM-dd"));


                            if (!bll.Add(model).Equals("0"))
                            {
                                //将文件复制到相应的课题组待审核的文件夹下
                                string newPath = ConfigurationManager.AppSettings["DiskPath"];
                                newPath += "Group/" + model.TEAM_ID + "/待审核";
                                string path = hidPath.Value;

                                string[] swfNames = path.Split('.');

                                string swfName = swfNames[0] + ".swf";
                                string pdffName = swfNames[0] + ".pdf";

                                FileHelper.Copy(path, newPath);
                                FileHelper.Copy(swfName, newPath);
                                FileHelper.Copy(pdffName, newPath);
                                lblError.Text = "提交成功";
                            }
                        }
                    }
                }
            }
        }

        #endregion 事件处理程序

        #region 函数集

        //页面加载函数 
        public void InitData()
        {



            BindTeamList();

            string File_ID = string.Empty;
            if (!string.IsNullOrEmpty(this.Request.QueryString["path"]))
            {
                hidPath.Value = this.Request.QueryString["path"].ToString();
            }
            //获取文档名称
            if (!string.IsNullOrEmpty(this.Request.QueryString["filename"]))
            {
                txtWDMC.Text = this.Request.QueryString["filename"].ToString();
            }
            if (!string.IsNullOrEmpty(this.Request.QueryString["File_ID"]))
            {
                File_ID = this.Request.QueryString["File_ID"].ToString();
            }
            if (!string.IsNullOrEmpty(File_ID))
            {
                TB_TEAM_FILEInfo model = new TB_TEAM_FILEInfo();
                TB_TEAM_FILEBiz bll = new TB_TEAM_FILEBiz();
                DataTable dt = bll.GetModel(File_ID);
                if (dt.Rows.Count > 0)
                {
                    hidIDS.Value = dt.Rows[0]["TEAM_ID"].ToString(); 
                    txtWDMC.Text = dt.Rows[0]["FILE_NAME"].ToString();
                    txtZYXX.Text = dt.Rows[0]["FILE_ABST"].ToString();
                    txtZZ.Text = dt.Rows[0]["FILE_AUTHOR"].ToString();
                    txtGJC.Text = dt.Rows[0]["KEYW"].ToString();
                    txtBXRQ.Text = dt.Rows[0]["FILE_CRT_TIME"].ToString();
                    txtMB.Text = dt.Rows[0]["DEST_OBJ"].ToString();
                    txtYWDX.Text = dt.Rows[0]["BIZ_OBJ"].ToString();

                }

            }

        }

        /// <summary>
        /// 绑定课题组
        /// </summary>
        public void BindTeamList()
        {
            string User_ID = Session["user_id"].ToString();

            Tb_TeamBiz teambll = new Tb_TeamBiz();
            DataTable dt = teambll.GetUTeamList(User_ID);
            if (dt.Rows.Count == 1)
            {
                cbTeamList.Visible = false;
                lblteam.Visible = true;
                hidIDS.Value = dt.Rows[0]["Team_ID"].ToString() + ";";
                lblteam.Text = dt.Rows[0]["Team_Name"].ToString();
            }
            else
            {
                cbTeamList.Visible = true;
                lblteam.Visible = false;
                cbTeamList.DataSource = dt;
                cbTeamList.DataBind();
            }
            
        }

        /// <summary>
        /// 文件复制
        /// </summary>
        /// <param name="source">源文件地址</param>
        /// <param name="destination">目标地址</param>
        /// <returns>成功返回true，失败返回false</returns>
        public bool FileCopy(string source, string destination)
        {
            bool ret = false;
            System.IO.FileInfo file_s = new System.IO.FileInfo(source);
            System.IO.FileInfo file_d = new System.IO.FileInfo(destination);
            if (file_s.Exists)
            {
                if (!file_d.Exists)
                {
                    file_s.CopyTo(destination);
                    ret = true;
                }
            }
            if (ret == true)
            {
                Response.Write("<script>alert('复制文件成功！');</script>");
            }
            else
            {
                Response.Write("<script>alert('复制文件失败！');</script>");
            }
            return ret;
        }

        #endregion 函数集


    }
}